package demo5;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/*public class DescIntWritable implements WritableComparable<DescIntWritable> {

    private int value;  // 存储数值

    public DescIntWritable() {
    }

    public DescIntWritable(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public int compareTo(DescIntWritable o) {
        return o.value - this.value; // 降序
    }

    @Override
    public void write(DataOutput out) throws IOException {
        // 序列化
        out.writeInt(value);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        // 反序列化
        value = in.readInt();
    }

    @Override
    public String toString() {
        return Integer.toString(value);
    }
}*/

// -----------------------------------

public class DescIntWritable extends IntWritable {
    // 直接继承 IntWritable 方法 跟自己实现 WritableComparable接口效果一样
    public DescIntWritable(){}

    public DescIntWritable(int value){
        super(value);
    }

    @Override
    public int compareTo(IntWritable o) {
        return o.get() - this.get();
    }
}
